package com.zzg.cloud.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class RespResult<T> implements Serializable {
    /**
     *  错误码
     */
    protected Integer code;
    /**
     * 错误信息
     */

    protected String msg;

    /**
     * data
     */
    protected T data;

    public RespResult(RespResultBuilder<T> builder) {
        this.code = builder.code;
        this.msg = builder.msg;
        this.data = builder.data;
    }

    public static <T> RespResult<T> succeed(){
        return new RespResult(BaseResultCode.SUCCESS_CODE, BaseResultCode.SUCCESS_MSG, null);
    }

    public static <T> RespResult<T> succeed(T data){
        return new RespResult(BaseResultCode.SUCCESS_CODE, BaseResultCode.SUCCESS_MSG, data);
    }

    public static <T> RespResult<T> succeed(String msg, T data){
        return new RespResult(BaseResultCode.SUCCESS_CODE, msg, data);
    }

    public static <T> RespResult<T> failed(String msg){
        return new RespResult(BaseResultCode.BASE_ERROR_CODE, msg, null);
    }

    public static <T> RespResult<T> failed(int code, String msg){
        return new RespResult(code, msg, null);
    }

    @JsonIgnore
    public Boolean isSucceed(){
        return BaseResultCode.SUCCESS_CODE.equals(code);
    }

    public static <T> RespResultBuilder<T> builder(){
        return new RespResultBuilder<T>();
    }

    public static class RespResultBuilder<T>{

        private Integer code;

        private String msg;

        private T data;

        public RespResultBuilder<T> code(int code){
            this.code = code;
            return this;
        }

        public RespResultBuilder<T> msg(String msg){
            this.msg = msg;
            return this;
        }

        public RespResultBuilder<T> data(T data){
            this.data = data;
            return this;
        }

        public RespResult<T> build(){
            return new RespResult(this);
        }

    }

}